import {
  promisic
} from "./util";
import {
  config
} from "../config/config";
import {
  showToast
} from "./ui";

class Http {
  static async request({
    url,
    data,
    method = 'GET'
  }) {
    let res;
    try {
      res = await promisic(wx.request)({
        url: `${config.apiBaseUrl}${url}`,
        data,
        method,
        header: {
          'content-type': 'application/json',
          'authorization': `Bearer ${wx.getStorageSync('token')}`
        }
      })
    } catch (e) {
      console.log(e, '请求错误');
      return null;
    }
    const code = res.statusCode.toString()
    if (code.startsWith('2')) {
      console.log(res.data.code.toString().startsWith('2'));
      return res.data
    } else {
      if (!(res.data.code.toString().startsWith('2'))) {
        showToast(res.data.msg)
      }
    }
    return res.data
  }
}

export {
  Http
}